package com.zhuzi.controller.dto;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.zhuzi.exception.ErrorCode;
import com.zhuzi.exception.ZhuziException;

/**
 * 客户端请求DTO对象。包括了客户端基本参数。请求参数封装在params变量中。
 */
public class ClientRequest implements Serializable {

	private static final long serialVersionUID = 1L;

	/** 用户标示,在需要用户信息的时候需要传递 */
	private String token;

	/** 请求唯一标示 */
	private String uuid;

	/** 客户端版本号 */
	private String clientVersion;

	/** 客户端类型 android,apple等 */
	private String client;

	/** 设备厂商 .ios的是apple。android的是厂商标识 */
	private String brand;

	/** 设备型号 */
	private String model;

	/** 客户端系统版本 */
	private String osVersion;

	/** 渠道编号 */
	private String channel;

	/** 网络连接类型 */
	private String networkType;

	/** 设备mac地址 */
	private String mac;

	/** 设备IMEI或apple设备的uuid */
	private String imei;

	/** ios设备推送使用的token */
	private String iosToken;

	/** 第三方推送个推所使用的设备编号 */
	private String clientId;

	/** 用户使用的是哪个app。工长助手:gongzhang,业主端:yezhu */
	private String app;

	/** apple设备。正式环境true,开发环境false */
	private Boolean apnsProduction;

	/** 业务参数 */
	private Map<String, Object> params;

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getClientVersion() {
		return clientVersion;
	}

	public void setClientVersion(String clientVersion) {
		this.clientVersion = clientVersion;
	}

	public String getClient() {
		return client;
	}

	public void setClient(String client) {
		this.client = client;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getOsVersion() {
		return osVersion;
	}

	public void setOsVersion(String osVersion) {
		this.osVersion = osVersion;
	}

	public String getChannel() {
		return channel;
	}

	public void setChannel(String channel) {
		this.channel = channel;
	}

	public String getNetworkType() {
		return networkType;
	}

	public void setNetworkType(String networkType) {
		this.networkType = networkType;
	}

	public String getMac() {
		return mac;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public String getIosToken() {
		return iosToken;
	}

	public void setIosToken(String iosToken) {
		this.iosToken = iosToken;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getApp() {
		return app;
	}

	public void setApp(String app) {
		this.app = app;
	}

	public Boolean getApnsProduction() {
		return apnsProduction;
	}

	public void setApnsProduction(Boolean apnsProduction) {
		this.apnsProduction = apnsProduction;
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	public String getStringParameter(String key) {
		return this.getStringParameter(key, false);
	}

	public String getStringParameter(String key, boolean strict) {
		if (null != params && null != params.get(key)) {
			String str = String.valueOf(params.get(key));
			if (StringUtils.isBlank(str) && strict) {
				throw new ZhuziException(ErrorCode.Paramter_Error, "缺少参数" + key);
			}
			return str;
		} else if (strict) {
			throw new ZhuziException(ErrorCode.Paramter_Error, "缺少参数" + key);
		}
		return null;
	}

	public String getStringParameter(String key, String def) {
		String str = this.getStringParameter(key, false);
		return StringUtils.isEmpty(key) ? def : str;
	}

	public Object getObjectParameter(String key) {
		return this.getObjectParameter(key, false);
	}

	public Object getObjectParameter(String key, boolean strict) {
		if (null != params && null != params.get(key)) {
			return params.get(key);
		} else if (strict) {
			throw new ZhuziException(ErrorCode.Paramter_Error, "缺少参数" + key);
		}
		return null;
	}

	public Integer getIntParameter(String key) {
		return this.getIntParameter(key, false);
	}

	public Integer getIntParameter(String key, Integer def) {
		Integer intParameter = this.getIntParameter(key, false);
		return intParameter == null ? def : intParameter;
	}

	public Integer getIntParameter(String key, boolean strict) {
		if (null != params && null != params.get(key)) {
			return Integer.parseInt(getStringParameter(key));
		} else if (strict) {
			throw new ZhuziException(ErrorCode.Paramter_Error, "缺少参数" + key);
		}
		return null;
	}

	public Long getLongParameter(String key) {
		return this.getLongParameter(key, false);
	}

	public Long getLongParameter(String key, boolean strict) {
		if (null != params && null != params.get(key)) {
			return Long.parseLong(getStringParameter(key));
		} else if (strict) {
			throw new ZhuziException(ErrorCode.Paramter_Error, "缺少参数" + key);
		}
		return null;
	}

	public Long getLongParameter(String key, Long def) {
		Long longParameter = this.getLongParameter(key, false);
		return longParameter == null ? def : longParameter;
	}

	public Double getDoubleParameter(String key) {
		return this.getDoubleParameter(key, false);
	}

	public Double getDoubleParameter(String key, boolean strict) {
		if (null != params && null != params.get(key)) {
			return Double.parseDouble(getStringParameter(key));
		} else if (strict) {
			throw new ZhuziException(ErrorCode.Paramter_Error, "缺少参数" + key);
		}
		return null;
	}

	public Date getDateParameter(String key) {
		return this.getDateParameter(key, false);
	}

	public Date getDateParameter(String key, boolean strict) {
		if (null != params && null != params.get(key)) {
			return new Date(getLongParameter(key));
		} else if (strict) {
			throw new ZhuziException(ErrorCode.Paramter_Error, "缺少参数" + key);
		}
		return null;
	}

	public BigDecimal getBigDecimalParameter(String key) {
		return this.getBigDecimalParameter(key, false);
	}

	public BigDecimal getBigDecimalParameter(String key, boolean strict) {
		if (null != params && null != params.get(key)) {
			return new BigDecimal(this.getStringParameter(key, true));
		} else if (strict) {
			throw new ZhuziException(ErrorCode.Paramter_Error, "缺少参数" + key);
		}
		return null;
	}

	public void setParams(String key, Object value) {
		if (this.params == null) {
			params = new HashMap<String, Object>();
		}
		params.put(key, value);
	}

	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
